//
//  BottomToolBar.m
//  EPTool
//
//  Created by shiqing guo on 2018/5/5.
//  Copyright © 2018年 云数动能科技. All rights reserved.
//

#import "BottomToolBar.h"
#import "UIView+DDAddition.h"
#import "UIControl+FixMultiClick.h"
#import "Macro.h"
#import "GlobalUtil.h"

#define W_BTN_BETWEEN 15.0f

@interface BottomToolBar () <UIActionSheetDelegate>

@end

@implementation BottomToolBar

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupDefault];
    }
    return self;
}


- (instancetype)initWithFrame:(CGRect)frame andBtnNames:(NSArray <NSString *>*)btnNames andTextColors:(NSMutableArray <UIColor *>*)textColors andBkColors:(NSMutableArray <UIColor *>*)bkColors andMaxCount:(NSUInteger)count
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = frame;
        self.backgroundColor = [UIColor clearColor];
        
        _btnNames = btnNames;
        _btnTextColors = textColors;
        _btnBkColors = bkColors;
        _maxBarBtnCount = count;
        
        [self setupDefault];
        [self createBtnsByBtnNames];
    }
    return self;
}

- (void)setupDefault {

    _maxBarBtnCount = (0 != _maxBarBtnCount)?_maxBarBtnCount:4;
    if (_btnNames.count > _btnTextColors.count) {
        for (int i = 0; i < _btnNames.count-_btnTextColors.count; i++) {
            [_btnTextColors addObject:[UIColor redColor]];
        }
    }
    if (_btnNames.count != _btnBkColors.count) {
        for (int i = 0; i < _btnNames.count-_btnBkColors.count; i++) {
            [_btnBkColors addObject:[UIColor whiteColor]];
        }
    }
}

- (void)setBtnNames:(NSArray<NSString *> *)btnNames andTextColors:(NSMutableArray <UIColor *>*)textColors andBkColors:(NSMutableArray <UIColor *>*)bkColors andMaxCount:(NSUInteger)count
{
    _btnNames = btnNames;
    _btnTextColors = textColors;
    _btnBkColors = bkColors;
    _maxBarBtnCount = count;
    [self updateInterface];
}

- (void)updateInterface {
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    [self createBtnsByBtnNames];
}


- (void)createBtnsByBtnNames {
    if (_btnNames && _btnNames.count > 0) {
        
        NSInteger showBtnCount = (_btnNames.count >= _maxBarBtnCount)?_maxBarBtnCount:_btnNames.count;
        CGFloat btnHeight = self.height;
        CGFloat btnWith = (self.width - (showBtnCount-1)*W_BTN_BETWEEN)/showBtnCount;
        
        for (int i = 0; i < _btnNames.count; i++) {
            
            // 设置按钮的文字
            NSString *btnName = _btnNames[i];
            if (i == _maxBarBtnCount-1 && _btnNames.count > _maxBarBtnCount) {
                btnName = @"更多";
            }
            // 创建按钮
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(i*(btnWith+W_BTN_BETWEEN), 0, btnWith, btnHeight)];
            [btn setTitle:btnName forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(onTapToolBarBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            btn.titleLabel.font = SystemFont(16.0f);
            btn.tag = i;
            // 设置按钮的样式
            UIColor *textColor = [_btnTextColors objectAtIndex:i];
            UIColor *bkColor = [_btnBkColors objectAtIndex:i];
            if (CGColorEqualToColor(bkColor.CGColor, [UIColor whiteColor].CGColor)) {
                btn.layer.borderWidth = UI_1_PX_SIZE;
                btn.layer.borderColor = [EPColor epColor_UnderLine].CGColor;
            }
            btn.cs_acceptEventInterval = 1;
            [GlobalUtil roundForView:btn withRadius:4.0f];
            [btn setTitleColor:textColor forState:UIControlStateNormal];
            [btn setBackgroundColor:bkColor];
            
            [self addSubview:btn];
            
            if (i == _maxBarBtnCount-1) {
                break;
            }
        }
    }
}


- (void)onTapToolBarBtnAction:(UIButton *)sender {
    
    if (sender.tag == _maxBarBtnCount-1 && _btnNames.count != _maxBarBtnCount) { //更多按钮
        
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:cancelAction];
        for (NSUInteger i = _maxBarBtnCount-1; i < _btnNames.count; i++) {
            UIAlertAction *action = [UIAlertAction actionWithTitle:_btnNames[i] style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                if (_tapToolBarBlock) {
                    NSString *titleName = _btnNames[i];
                    _tapToolBarBlock(0, titleName);
                }
            }];
            [alert addAction:action];
        }
        // 解决ipad闪退问题
        if ([alert respondsToSelector:@selector(popoverPresentationController)]) {
            alert.popoverPresentationController.sourceView = self.viewController.view;
        }
        [self.viewController presentViewController:alert animated:YES completion:nil];
        
    } else {
        if (_tapToolBarBlock) {
            _tapToolBarBlock(sender.tag, sender.titleLabel.text);
        }
    }
}

@end
